#!/bin/bash

# 使用Docker构建RuoYi-TDesign项目的脚本
# 避免在宿主机上安装Node.js和Maven

echo "开始使用Docker构建 RuoYi-TDesign 项目..."

# 检查Docker是否已安装
if ! command -v docker &> /dev/null; then
    echo "安装 Docker..."
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    sudo systemctl start docker
    sudo systemctl enable docker
fi

# 检查Docker Compose是否已安装
if ! command -v docker-compose &> /dev/null; then
    echo "安装 Docker Compose..."
    sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi

# 配置Docker镜像加速器（使用阿里云加速器）
echo "配置Docker镜像加速器..."
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://ydosa7k8.mirror.aliyuncs.com",
    "https://registry.cn-hangzhou.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com"
  ]
}
EOF

# 重启Docker服务
echo "重启Docker服务..."
sudo systemctl daemon-reload
sudo systemctl restart docker

# 创建必要的目录
echo "创建必要的目录..."
sudo mkdir -p /docker/mysql/data /docker/mysql/conf
sudo mkdir -p /docker/redis/data /docker/redis/conf
sudo mkdir -p /docker/minio/data /docker/minio/config
sudo mkdir -p /docker/nginx/cert /docker/nginx/conf.d /docker/nginx/html /docker/nginx/log
sudo mkdir -p /docker/admin/logs /docker/server2/logs /docker/monitor/logs /docker/snailjob/logs

# 首先构建本地依赖模块（使用明确支持JDK 17的Maven镜像）
echo "构建本地依赖模块..."
docker run --rm \
  -v "$(pwd)":/app \
  -v ~/.m2:/root/.m2 \
  -w /app \
  maven:3.8.4-openjdk-17 \
  mvn clean install -DskipTests -Drevision=1.4.0 || { echo "本地依赖模块构建失败"; exit 1; }

# 使用Maven Docker镜像并行构建所有后端服务
echo "并行构建后端服务..."
(
  # 构建后端服务
  docker run --rm \
    -v "$(pwd)":/app \
    -v ~/.m2:/root/.m2 \
    -w /app/ruoyi-admin \
    maven:3.8.4-openjdk-17 \
    mvn clean package -DskipTests -Drevision=1.4.0 || { echo "后端服务构建失败"; exit 1; }
) &

(
  # 构建监控服务
  docker run --rm \
    -v "$(pwd)":/app \
    -v ~/.m2:/root/.m2 \
    -w /app/ruoyi-extend/ruoyi-monitor-admin \
    maven:3.8.4-openjdk-17 \
    mvn clean package -DskipTests -Drevision=1.4.0 || { echo "监控服务构建失败"; exit 1; }
) &

(
  # 构建任务调度服务
  docker run --rm \
    -v "$(pwd)":/app \
    -v ~/.m2:/root/.m2 \
    -w /app/ruoyi-extend/ruoyi-snailjob-server \
    maven:3.8.4-openjdk-17 \
    mvn clean package -DskipTests -Drevision=1.4.0 || { echo "任务调度服务构建失败"; exit 1; }
) &

# 等待所有后台任务完成
wait

# 使用Node.js Docker镜像构建前端 (使用npm镜像加速)
echo "构建前端服务..."
docker run --rm \
  -v "$(pwd)":/app \
  -w /app/ruoyi-ui \
  node:18 \
  sh -c "npm config set registry https://registry.npmmirror.com && npm install && npm run build" || { echo "前端构建失败"; exit 1; }

# 并行构建所有Docker镜像
echo "并行构建Docker镜像..."

(
  # 构建后端服务镜像
  echo "构建 ruoyi-server 镜像..."
  cd ruoyi-admin
  docker build -t ruoyi/ruoyi-server:1.4.0 . || { echo "后端Docker镜像构建失败"; exit 1; }
  cd ..
) &

(
  # 构建监控服务镜像
  echo "构建 ruoyi-monitor-admin 镜像..."
  cd ruoyi-extend/ruoyi-monitor-admin
  docker build -t ruoyi/ruoyi-monitor-admin:1.4.0 . || { echo "监控服务Docker镜像构建失败"; exit 1; }
  cd ../..
) &

(
  # 构建任务调度服务镜像
  echo "构建 ruoyi-snailjob-server 镜像..."
  cd ruoyi-extend/ruoyi-snailjob-server
  docker build -t ruoyi/ruoyi-snailjob-server:1.4.0 . || { echo "任务调度服务Docker镜像构建失败"; exit 1; }
  cd ../..
) &

(
  # 构建前端服务镜像
  echo "构建 ruoyi-ui 镜像..."
  cd ruoyi-ui
  docker build -t ruoyi/ruoyi-ui:1.4.0 . || { echo "前端Docker镜像构建失败"; exit 1; }
  cd ..
) &

# 等待所有镜像构建完成
wait

# 启动所有服务
echo "启动所有服务..."
docker-compose up -d

echo "部署完成！"
echo "查看服务状态: docker-compose ps"
echo "查看日志: docker-compose logs -f"